package at.fhj.swd11.leitner.ue4;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class PlusMinus extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8543455317664899267L;
	/**
	 * Project 4 - PlusMinus
	 * @author Stefan Leitner
	 */
	
	// Buttons
	private JButton bPlus = new JButton("+");
	private JButton bMinus = new JButton("-");
	private JButton bExit = new JButton("EXIT");
	
	// Label
	private JLabel lblCount = new JLabel("0");
	
	// Variablen
	private int count = 0;
	
	public static void main(String[] args) {
		/**
		 * creates new window
		 *
		 */
		PlusMinus wnd = new PlusMinus();
		wnd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		wnd.setSize(300, 150);
		wnd.setVisible(true);

	}
	
	public PlusMinus(){
		super("Plus - Minus");
		
		// Panel Number
		JPanel pNum = new JPanel();
		pNum.add(lblCount);
		
		// Panel Buttons
		JPanel pButtons = new JPanel();
		pButtons.setLayout(new GridLayout(3,1));
		pButtons.add(bPlus);
		pButtons.add(bMinus);
		pButtons.add(bExit);
		
		// ContentPane
		this.getContentPane().setLayout(new GridLayout(1,2));
		this.getContentPane().add(pNum);
		this.getContentPane().add(pButtons);
		
		// ActionCommands
		this.bPlus.setActionCommand("P");
		this.bMinus.setActionCommand("M");
		this.bExit.setActionCommand("E");
		
		this.bPlus.addActionListener(this);
		this.bMinus.addActionListener(this);
		this.bExit.addActionListener(this);
		
				
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		String command = e.getActionCommand();
		
		if(command.equalsIgnoreCase("P")){
			this.count++;
			this.lblCount.setText(""+this.count+"");
		}
		if(command.equalsIgnoreCase("M")){
			this.count--;
			this.lblCount.setText(""+this.count+"");
		}
		if(command.equalsIgnoreCase("E")){
			System.exit(0);
		}

	}

}
